<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Config;
use think\Request;
use think\Validate;

class HotelRoomType extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    protected function filter(&$map)
    {
        if ($this->request->param("room_name")) {
            $map['room_name'] = ["like", "%" . $this->request->param("room_name") . "%"];
        }
        if ($this->request->param("create_time")) {
            $start_create_time = strtotime($map['create_time'].' 0:0:0');
            $end_create_time = strtotime($map['create_time'].' 23:59:59');
            $map['create_time'] = ['between',[$start_create_time,$end_create_time]];
        }
    }
    /*
     * 添加
     */
    public function add(Request $request)
    {
        $data = input();

        if ($request->isPost()){
            // 处理post提交数据
        }else{
            // 渲染模板
            return $this->view->fetch('edit');
        }

        if ($this->request->isAjax()) {
//            if (!$data['id']) {
//                $data['id'] = get_id();
//            }
            $data['create_time'] = NOW_TIME;
            $hid = Db::name('hotel_room_type')->insertGetId($data);
            $hotel_room_types = [];

            if(is_array($data['$hotel_room_types']['name'])){
                foreach ($data['$hotel_room_types']['name'] as $key=>$vle){

                    $hotel_room_types['room_name']=$data['hotel_room_type']['room_name'][$key];
                    $hotel_room_types['remark']=$data['hotel_room_type']['remark'][$key];


                    $hotel_room_types['hid']=$hid;
                    $hotel_room_types[]=$hotel_room_types;


                }
            }
            if($hid){
                if($hotel_room_types){
                    $re=Db::name('hotel_room_type')->insertAll($hotel_room_types);
                    if($re){
                        return ajax_return_adv("添加成功");
                    }
                }else{
                    return ajax_return_adv("添加成功");
                }

            }
            return ajax_return_error("添加失败");
        }else{
            return ajax_return_error("暂无数据");
        }
    }

    /*
     * 添加房型
     * */
    public function addType(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            // 验证数据
            if (!$postData['room_name']){
                return ajax_return_adv_error('房型名称不能为空');
            }
            $find = Db::name('hotel_room_type')->where('room_name',$postData['room_name'])->find();
            if ($find){
                return ajax_return_adv_error('已存在该房型数据');
            }
            $postData['create_time'] = time();
            $add = Db::name('hotel_room_type')->strict(false)->insertGetId($postData);
            if ($add){
                return ajax_return_adv('添加成功');
            }else{
                return ajax_return_adv_error('添加失败');
            }
        }else{
            return ajax_return_adv_error('非法请求');
        }
    }

    /**
     * 更新房型数据
     */
    public function save(Request $request)
    {
        if ($request->isPost()){
            $postData = $request->post();
            // 验证数据
            if (!$postData['room_name']){
                return ajax_return_adv_error('房型名称不能为空');
            }
            if (!$postData['id']){
                return ajax_return_adv_error('缺少必要的参数');
            }
            $find = Db::name('hotel_room_type')->where('id','<>',$postData['id'])->where('room_name',$postData['room_name'])->find();
            if ($find){
                return ajax_return_adv_error('已存在该房型名称');
            }
            $up = Db::name('hotel_room_type')->strict(false)->update($postData);
            $find = Db::name('hotel_room_type')->where('id',$postData['id'])->find();
            if ($up || $find['room_name'] == $postData['room_name']){
                return ajax_return_adv('更新成功');
            }else{
                return ajax_return_adv_error('更新失败');
            }
        }else{
            return ajax_return_adv_error('非法请求');
        }
    }
}
